package com.fieldchain.ex.fa100.access;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;

import com.fieldchain.ex.fa100.utils.third.chart.QiyuImageLoader;
import com.qiyukf.unicorn.api.StatusBarNotificationConfig;
import com.qiyukf.unicorn.api.UICustomization;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.screen.ScreenUtils;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.fieldchain.ex.fa100.utils.share.ShareUtils;

public class MyApplication extends Application {
    private static MyApplication appContext;
    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;
        init();
    }

    public static MyApplication getAppContext() {
        if (null == appContext) {
            appContext = new MyApplication();
        }
        return appContext;
    }

    private void init() {
        HttpRequest.getInstance().init();
        ShareUtils.getInstance().init();
        screenInit();
        aluminit();
        refreshInit();
        setDeviceId();
        Unicorn.init(this, "七鱼客服appKey", options(), new QiyuImageLoader(appContext));
    }

    public void setDeviceId() {
        String androidID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        String id = androidID + Build.SERIAL;

        ShareManager.getInstance().saveDeviceId(id);
    }

    //屏幕宽高获取并保存
    private void screenInit() {
        ShareManager.getInstance().saveWidth(ScreenUtils.getScreenWidth(appContext));
        int statusBarHeight = ScreenUtils.getStatusBarHeight(appContext);
        ShareManager.getInstance().saveHeight(ScreenUtils.getScreenHeight(appContext) - statusBarHeight);
    }

    //相册初始化
    private void aluminit() {
//        Album.initialize(AlbumConfig.newBuilder(this).setAlbumLoader(new MediaLoader()).build());
    }

    //刷新
    private void refreshInit() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                return new ClassicsFooter(context);
            }
        });
    }
    private YSFOptions options() {
        YSFOptions options = new YSFOptions();
        options.uiCustomization=new UICustomization();
        options.uiCustomization.hideKeyboardOnEnterConsult=true;
        options.statusBarNotificationConfig = new StatusBarNotificationConfig();
        return options;
    }

}
